// const util = require('./utils/httpUtils.js')
App({
    onLaunch: function(options) {
        // 判断是否由分享进入小程序
        if (options.scene == 1007 || options.scene == 1008 || options.scene == 1047) {
            this.globalData.share = true
        } else {
            this.globalData.share = false
        };
        //获取设备顶部窗口的高度（不同设备窗口高度不一样，根据这个来设置自定义导航栏的高度）
        wx.getSystemInfo({
            success: (res) => {
                this.globalData.height = res.statusBarHeight 
            }
        })

        // 设置定位存储缓存
        // util.getLocation(res => {
        //     let _data = res.originalData.result.addressComponent
        //     try {
        //         wx.setStorageSync('address', _data.city)
        //     } catch (e) {
        //         console.log(e)
        //     }
        // }, fail => {
        //     wx.setStorage({
        //         key: 'address',
        //         data: '郑州市',
        //     })
        // })
        this.Login(); // 微信登录  获取openid

        try { // 获取token 使用同步方法,否则可能会出现获取不到情况
            const token = wx.getStorageSync('token')
            if (token) {
                this.globalData.token = token.Authorization
            } else {
                this.returnLogin()
            }

        } catch (e) {
            console.log(e)
        }

        try { //同步获取 定位城市
            const city = wx.getStorageSync('address')
            if (city) {
                this.globalData.city = city
            } else {
                this.globalData.city = '郑州市'
            }
        } catch (e) {
            console.log(e)
        }
    },
    Login() {
        wx.login({
            success: res => { //获取openid
                let code = res.code
                wx.request({
                    url: this.globalData.host + '/api/Wxpay/getOpen?code=' + code,
                    success: res => {
                        wx.setStorageSync('openid', res.data.openid)
                        wx.setStorageSync('session_key', res.data.session_key)
                        this.globalData.openId = res.data.openid;
                        this.globalData.session_key = res.data.session_key
                    }
                })
            }
        })
    },
    returnLogin() {
        wx.showModal({
            title: '提示',
            content: '您还没有登陆,部分功能暂不可用',
            confirmText: '登录',
            cancelText: '暂不',
            success: res => {
                if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/public/login/login',
                    })
                }
            }
        })
    },
    globalData: {
        userInfo: {},
        host: 'https://app.zhijianjiaxiang.com',
        // host: 'http://test.zhijianjiaxiang.com',
        token: '',
        openId: '',
        session_key: '',
        city: '',
        share: false, // 分享默认为false
        publicKey: `-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwt5UGryfWj0BCZBFjJKi5kfDqktfjKmQXSHw9ZkbbcUjow8xR4EQPXjcm64OqLoboxVOdFZBJCc36AOcABP3Es1ZgXFzV9CJ1HCO8zAtW6jba089sGLULCiCKZMgtTlTtcNEYC/btArEppJcFi3wPnjynDsC7wvUT3u7H3C0D083Q+E7RRqMrTR5XmtPN8R8YW0ECrCcpHq04E0e6pdpU3OTRQ15sqR3M2fcBO8M7J+UN4zsycKILTTmyU9Ph/CaALcSU/4FlQ8YW1fU47lnjgLkwwbR5FFWVcbNIzhIo0cv69h/IPVF2noPd6a1kIWxK7edubP0jT0gj3B2+z2BvwIDAQAB-----END PUBLIC KEY-----`,
        height: 0
    }
})